package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.entity.Notice;
import cn.js.sangu.unitStore.entity.User;
import cn.js.sangu.unitStore.service.NoticeService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：公告接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("notice")
@RestController
public class NoticeController {

    static final String OPERATION_NOTICE = "公告模块";

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize,
                        @RequestParam String title,
                        @RequestParam String content){
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(title),"title",title);
        wrapper.like(StrUtil.isNotBlank(content),"content",content);
        wrapper.orderByDesc("id");

        //构建page对象
        Page<Notice> param = new Page<>(pageNum, pageSize);
        Page<Notice> res = noticeService.page(param, wrapper);
        //补充发布人信息
        res.getRecords().forEach(notice -> notice.setUsername(userService.getById(notice.getUserId()).getName()));
        return R.ok(res);
    }

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody Notice dto){
        Notice notice = noticeService.saveInfo(dto);
        return R.ok("《"+notice.getTitle()+"》保存成功");
    }

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.UPDATE)
    @PutMapping("update")
    public R update(@RequestBody Notice dto){
        noticeService.updateInfo(dto);
        return R.ok("修改成功");
    }

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.DELETE)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        noticeService.removeById(id);
        return R.ok("删除成功");
    }

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.BATCH_DELETE)
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        noticeService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

    @MyLogsAOP(operation = OPERATION_NOTICE,operaType = LogType.LOOKUP)
    @GetMapping("findById/{id}")
    public R findById(@PathVariable Integer id){
        Notice notice = noticeService.getById(id);
        User user = userService.getById(notice.getUserId());
        if (null != user){
            notice.setUsername(user.getName());
        }
        return R.ok(notice);
    }

    @GetMapping("findUserNotice")
    public R findUserNotice(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.eq("is_open",1); //用户只能看到1已公开的公告
        List<Notice> list = noticeService.list(queryWrapper);
        return R.ok(list);
    }

}
